package com.qingfeng.workflow.test;

//import com.qingfeng.common.service.SkdmClient;
import com.qingfeng.utils.ResponseResult;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cb
 * @Description TODO
 * @date 2023/5/16
 */
@SpringBootTest
public class test {
//    @Autowired
//    private SkdmClient skdmClient;

    @Autowired
    private RuntimeService runtimeService;

    @Test
    public void completeTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. 获取 TaskService
        TaskService taskService = processEngine.getTaskService();
        System.out.println("taskService:   " +taskService);
// 3. 根据流程唯一标识 key 和 任务办理人 查询任务
        TaskQuery query = taskService.createTaskQuery().processDefinitionId("Process_1684400563683:2:185de885-f55b-11ed-9ad0-5c619987c6da")
                // 流程 Key
//.taskAssignee("meng") // 查询 meng 的任务
                .taskAssignee("caiwu1"); // 目前只有一条任务，则可以只获取一条
// 4. 完成任务（任务id）

        List<Task> taskList = query.list();
        System.out.println("taskList:   " +taskList);
        for (Task task : taskList) {
            try {

                taskService.complete(task.getId());

            } catch (Exception e) {

                System.out.println(e.getMessage());
            }
        }
    }

    @Test
    public void startProcessByIdWithVar() {
        // RuntimeService runtimeService = processInstanceService.getRuntimeService();
        List<String> assigneeList = new ArrayList<String>();


        assigneeList.add("caiwu1");
        assigneeList.add("caiwu2");
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("assigneeList", assigneeList);
//        ProcessInstance pi = runtimeService.startProcessInstanceById("Process_1684393351511:2:ea0340fb-f54a-11ed-826e-5c619987c6da", vars);
        ProcessInstance pi = runtimeService.startProcessInstanceById("Process_1684400563683:2:185de885-f55b-11ed-9ad0-5c619987c6da", vars);
        System.out.println("流程定义ID：" + pi.getProcessInstanceId());

        System.out.println("流程实例ID：" + pi.getId());
    }

//    @Test
//   public void  gBase(){
//
//     ResponseResult bb=skdmClient.releaseDataBase("KDM_1686022607856_127");
//
//
//   }



}
